package com.web.base.auth.service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.blue.jdbc.Page;
import com.web.base.auth.dao.RoleDao;
import com.web.base.auth.dao.RoleUserDao;
import com.web.base.auth.model.Role;
import com.web.base.auth.model.RoleUser;
import com.web.core.util.Const;

/**
 * 角色业务层
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2017年04月15日
 */
@Service
@Transactional
public class RoleService
{
	@Resource
	private RoleDao roleDao;
	@Resource
	private RoleUserDao roleUserDao;
	
	public RoleService()
	{
	}
	
	public Role save(Role role)
	{
		if (role.getId() == null)
		{
			roleDao.save(role);
		}
		else
		{
			roleDao.update(role);
		}
		Role a = roleDao.get(role.getId());
		return a;
	}
	
	public Role get(Integer id)
	{
		Role log = roleDao.get(id);
		return log;
	}
	
	public Page<Role> list(Role role, Page<Role> page)
	{
		page = roleDao.list(role, page);
		return page;
	}
	
	public List<Role> list(Role role)
	{
		List<Role> list = roleDao.list(role);
		return list;
	}
	
	public void delete(Role role)
	{
		roleDao.delete(role);
	}
	
	public void addRoleUser(Integer userId, String roleIds)
	{
		roleUserDao.deleteBy("userId", userId);
		if (roleIds != null && !roleIds.isEmpty())
		{
			List<RoleUser> ruList = Stream.of(roleIds.split(Const.ID_LIST_SPLIT))
					.map(e -> new RoleUser(Integer.valueOf(e), userId))
					.collect(Collectors.toList());
			roleUserDao.saveList(ruList);
		}
	}
	
	public List<Role> listSelect(Integer userId)
	{
		List<Role> list = roleUserDao.listSelect(userId);
		return list;
	}
	
	public List<RoleUser> listRoleUser(RoleUser roleUser)
	{
		List<RoleUser> list = roleUserDao.list(roleUser);
		return list;
	}
	
}
